const app = getApp();
var url = app.getURL();

// 灰色头像
const defaultAvatarUrl = 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0';
Page({
  data: {
    hasUserInfo: false,
    canIUseGetUserProfile: false,
    avatarUrl: defaultAvatarUrl,
    nickName: '',
    list:[],
  },

  onLoad() {
    /* 从缓存中取出去数据 */
    let that = this
    let user = app.getGlobalUserInfo();
    if(user.cookieToken!=''){
      console.log("user:",user);
      that.setData({
        avatarUrl:user.cookieAvatar,
        nickName: user.cookieNickname,
        hasUserInfo:true,
      })
      // 定义全局变量
        app.globalData.userInfo.avatarUrl= wx.getStorageSync('cookieAvatar'),
        app.globalData.userInfo.nickName = wx.getStorageSync('cookieNickname')
    }
    console.log(app.globalData.userInfo)
    this.getUserForm()
  },

  getUserProfile(e) {
    let that = this;
    wx.getUserProfile({
      desc: '授权登录', 
      success: (res) => {
        // 存入全局变量
        app.globalData.userInfo=res.userInfo;
        console.log(app.globalData)
        // 获得头像昵称
        that.setData({
          nickName:res.userInfo.nickName,
          avatarUrl:res.userInfo.avatarUrl,
        })
        wx.login({  
          success (res) {
            if (res.code) {//发送本地获取code
              console.log(res);
                wx.request({
                    //发送openid
                    method:"POST",
                    url: url+'/user/app/cust/wxLogin',
                    data:{
                      code: res.code,
                      nickName: that.data.nickName,
                      avatarUrl: that.data.avatarUrl,
                    },
                    success(res){
                      console.log(res)
                      let result= res.data.data
                      // 存入缓存
                      app.setGlobalUserInfo(result.token,result.nickName,that.data.avatarUrl);
                      that.setData({
                        hasUserInfo:true
                      })
                      wx.setStorageSync('message',result)
                      wx.setStorageSync('cookieToken', result.token)
                      wx.setStorageSync('cookieNickname', result.nickName)
                      if(res.data.code!=200){
                        wx.showToast({
                          title: '登录失败',
                          icon: 'none',
                          duration: 600
                        })
                      }

                      this.getUserForm();
                    }
                  })
            } else {
          console.log('登录失败！' + res.errMsg)
          wx.showToast({
            title: 'code返回失败',
            icon: 'none',
            duration: 600
          })
        }
      },
      fail:()=>{
        wx.showToast({
        title: '登录失败',
        icon: 'none',
        duration: 600
      })
      } 
        })
      
      },fail:()=>{
        console.log("用户拒绝授权");
        wx.showToast({
          title: '用户拒绝授权',
          icon: 'none',
          duration: 600
        })
      }
    });
  },
  getUserForm(){
    let that = this;
    wx.request({
      url: url+'/app/user/formList',
      method: "GET",
      header: {
        'content-type': 'application/json',
        token:wx.getStorageSync('cookieToken')
      },
      success(res) {
        console.log(res.data)
        that.setData({
          "list": res.data.data
        })
      }
    })
  },
  // 分享微信好友
  onShareAppMessage: function () {
  },
})